package com.example.smssender;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 短信发送记录数据模型
 */
public class SmsSendRecord implements Serializable {
    private String id;                          // 记录ID（时间戳）
    private long timestamp;                      // 发送时间戳
    private String dateTime;                     // 格式化时间（如：2024-01-15 14:30:25）
    
    // 统计信息
    private int totalContacts;                   // 总联系人数量
    private int successCount;                    // 成功数量
    private int failCount;                       // 失败数量
    private double successRate;                  // 成功率（百分比）
    
    // 发送内容
    private String messageContent;               // 短信内容（完整内容，不截断）
    private String templateId;                   // 使用的模板ID（如果有）
    private String templateName;                 // 使用的模板名称（如果有）
    
    // 联系人详情
    private List<ContactInfo> successContacts;   // 成功发送的联系人列表
    private List<FailedContact> failedContacts;  // 失败的联系人列表
    
    // 系统信息
    private String smscAddress;                  // 短信中心号码（发送时检测）
    private boolean smscConfigured;               // 短信中心号码是否配置
    private String deviceInfo;                   // 设备信息（可选）

    public SmsSendRecord() {
        this.id = String.valueOf(System.currentTimeMillis());
        this.timestamp = System.currentTimeMillis();
        this.dateTime = formatDateTime(this.timestamp);
        this.successContacts = new ArrayList<>();
        this.failedContacts = new ArrayList<>();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
        this.dateTime = formatDateTime(timestamp);
    }

    public String getDateTime() {
        return dateTime;
    }

    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }

    public int getTotalContacts() {
        return totalContacts;
    }

    public void setTotalContacts(int totalContacts) {
        this.totalContacts = totalContacts;
        // 自动计算成功率
        calculateSuccessRate();
    }

    public int getSuccessCount() {
        return successCount;
    }

    public void setSuccessCount(int successCount) {
        this.successCount = successCount;
        // 自动计算成功率
        calculateSuccessRate();
    }
    
    /**
     * 计算成功率（确保数据一致性）
     */
    private void calculateSuccessRate() {
        if (totalContacts > 0) {
            this.successRate = (double) successCount / totalContacts * 100;
        } else {
            this.successRate = 0;
        }
    }

    public int getFailCount() {
        return failCount;
    }

    public void setFailCount(int failCount) {
        this.failCount = failCount;
    }

    public double getSuccessRate() {
        return successRate;
    }

    public void setSuccessRate(double successRate) {
        this.successRate = successRate;
    }

    public String getMessageContent() {
        return messageContent;
    }

    public void setMessageContent(String messageContent) {
        // 保存完整内容，不截断
        this.messageContent = messageContent;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getTemplateName() {
        return templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }

    public List<ContactInfo> getSuccessContacts() {
        // 确保返回非null列表（防止反序列化时字段为null）
        if (successContacts == null) {
            successContacts = new ArrayList<>();
        }
        return successContacts;
    }

    public void setSuccessContacts(List<ContactInfo> successContacts) {
        this.successContacts = successContacts != null ? successContacts : new ArrayList<>();
    }

    public List<FailedContact> getFailedContacts() {
        // 确保返回非null列表（防止反序列化时字段为null）
        if (failedContacts == null) {
            failedContacts = new ArrayList<>();
        }
        return failedContacts;
    }

    public void setFailedContacts(List<FailedContact> failedContacts) {
        this.failedContacts = failedContacts != null ? failedContacts : new ArrayList<>();
    }

    public String getSmscAddress() {
        return smscAddress;
    }

    public void setSmscAddress(String smscAddress) {
        this.smscAddress = smscAddress;
    }

    public boolean isSmscConfigured() {
        return smscConfigured;
    }

    public void setSmscConfigured(boolean smscConfigured) {
        this.smscConfigured = smscConfigured;
    }

    public String getDeviceInfo() {
        return deviceInfo;
    }

    public void setDeviceInfo(String deviceInfo) {
        this.deviceInfo = deviceInfo;
    }

    /**
     * 格式化时间戳为可读的日期时间字符串
     */
    private String formatDateTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date(timestamp));
    }

    /**
     * 获取友好的时间显示（今天/昨天/具体日期）
     */
    public String getFriendlyDateTime() {
        long now = System.currentTimeMillis();
        long diff = now - timestamp;
        long oneDay = 24 * 60 * 60 * 1000;

        if (diff < oneDay) {
            // 今天
            SimpleDateFormat sdf = new SimpleDateFormat("今天 HH:mm", Locale.getDefault());
            return sdf.format(new Date(timestamp));
        } else if (diff < 2 * oneDay) {
            // 昨天
            SimpleDateFormat sdf = new SimpleDateFormat("昨天 HH:mm", Locale.getDefault());
            return sdf.format(new Date(timestamp));
        } else {
            // 具体日期
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
            return sdf.format(new Date(timestamp));
        }
    }
}

